package leetcode.code0442;

import java.util.ArrayList;
import java.util.List;

import leetcode.IDebug;
import leetcode.helper.HelpDebug;

//执行用时：6 ms, 在所有 Java 提交中击败了51.70%的用户
//
//内存消耗：49.3 MB, 在所有 Java 提交中击败了10.33%的用户
//
//通过测试用例：28 / 28
public class Solution implements IDebug {

	public List<Integer> findDuplicates(int[] nums) {
		int p = 0;
		int len = nums.length;
		List<Integer> ans = new ArrayList<>();
		while (p < len) {
			int cur = nums[p];
			if (cur != 0 && cur != -1) {
				if (nums[cur - 1] == -1) {
					nums[cur - 1] = 0;
					nums[p] = -1;
				} else if (nums[cur - 1] == 0) {
					ans.add(cur);
					nums[p] = -1;
					p++;
				} else if (cur - 1 == p) {
					nums[p] = 0;
					p++;
				} else {
					int tmp = nums[cur - 1];
					nums[cur - 1] = 0;
					nums[p] = tmp;
				}
			} else {
				p++;
			}
		}
		return ans;
	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();
		so.findDuplicates(HelpDebug.str2array3("[1,1,2]"));
		so.findDuplicates(HelpDebug.str2array3("[4,3,2,7,8,2,3,1]"));

	}

}
